﻿// ## 2020/09/28 # 氕氘氚 # 重构 ##

namespace FIRE_DAMAGE
{
	// 火计伤害；火陷阱第二段伤害；火矢第二段伤害
	//---------------------------------------------------------------------------


	//---------------------------------------------------------------------------

	class Main
	{
		Main()
		{
			pk::set_func(214, pk::func214_t(callback));
		}

		void callback(pk::damage_info& info, pk::unit@ attacker, pk::hex_object@ target, bool critical)
		{

			pk::force@ target_force = pk::get_force(target.get_force_id());
			int damage = 0;

			if (target.is_instance(pk::unit::type_id))
			// 部队
			{
				pk::unit@ target_unit = pk::hex_object_to_unit(target);

				damage = 部队基础火焰伤害;

				if (pk::is_alive(attacker))
				{
					if (attacker.has_tech(技巧_爆药炼成))
					{
						damage += 部队火伤_练成炸药附加;
						info.atk_tech = 技巧_爆药炼成;
					}
					if (ch::has_skill(attacker, 特技_火神))
						info.atk_skill = 特技_火神;
					else if (ch::has_skill(attacker, 特技_火攻))
						info.atk_skill = 特技_火攻;
				}
				if (pk::is_alive(target_unit))
				{
					if (ch::has_skill(target_unit, 特技_藤甲))
						info.def_skill = 特技_藤甲;
					else if (ch::has_skill(target_unit, 特技_火神))
						info.def_skill = 特技_火神;
					else if (ch::has_skill(target_unit, 特技_辟火))
						info.def_skill = 特技_火神;
				}
				damage = batt::fire_skill_impacts(damage, attacker, target_unit);	//计算伤害修正
				damage = int(damage * ch::randfloat(0.8, 1.2));

				info.troops_damage = damage;
			}
			else
			// 建筑
			{
				pk::building@ building = pk::hex_object_to_building(target);

				// 提防防止伤害
				if (building.facility == 시설_제방 and !building.completed)
				{
					info.hp_damage = 0;
					return;
				}

				damage = 建筑基础火焰伤害;

				if (pk::is_alive(attacker))
				{
					if (attacker.has_tech(技巧_爆药炼成))
					{
						damage += 建筑火伤_练成炸药附加;
						info.atk_tech = 技巧_爆药炼成;
					}
					if (ch::has_skill(attacker, 特技_火神))
						info.atk_skill = 特技_火神;
					else if (ch::has_skill(attacker, 特技_火攻))
						info.atk_skill = 特技_火攻;
				}
				damage = batt::fire_atk_skill_impact(damage, attacker);	//计算伤害修正
				damage = int(damage * ch::randfloat(0.8, 1.2));

				// 府的耐久不能掉到0
				int spec_id = ch::to_spec_id(target.get_id());
				if (ch::is_valid_spec_id(spec_id))
				{
					damage = pk::min(building.hp - 1, damage);
					int troops_damage = int(人员基础燃烧伤害 * ch::randfloat(0.8, 1.2));
					//pk::trace(pk::format("spec_id{},", spec_id));
					troops_damage += int(ch::get_spec_p(spec_id).troops * 人员燃烧比例伤害 / 100.f);
					troops_damage = pk::min(troops_damage, ch::get_spec_p(spec_id).troops);
					if (troops_damage > 0)
					{
						ch::get_spec_p(spec_id).add_troops(-troops_damage);
						pk::combat_text(-troops_damage, 1, target.get_pos());
					}

				}

				info.hp_damage = damage;
			}

			info.tactics = -1;
			if (pk::is_alive(attacker))
				info.src_pos = attacker.get_pos();
			info.dst_pos = target.get_pos();

			//pk::trace(pk::format("214 FIRE_DAMAGE {} {}", info.troops_damage, info.hp_damage));
		}
	}

	Main main;
}